package com.gzsxy.JUC.Callable;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class CallableTest {
    public static void main(String[] args) throws ExecutionException, InterruptedException {


//        new Thread(new Runable()).start();
//        new Thread(new FutureTask<V>()).start();
//        new Thread(new FutureTask<V>(callable)).start();

        MyThread myThread = new MyThread();
        //适配类
        FutureTask futureTask = new FutureTask<>(myThread);

        new Thread(futureTask, "A").start();
        new Thread(futureTask, "B").start(); //结果会缓存，提供效率
        //获取callable的返回结果
        Integer o = (Integer) futureTask.get(); //这个get方法可能会产生阻塞，把它放在最后

        //或者使用异步通信来处理！
        System.out.println(o);
    }

}


class MyThread implements Callable<Integer> {


    @Override
    public Integer call() throws Exception {
        System.out.println("call()");
        return 1024;
    }

}